<?php  if (!defined('PATH_HD')) exit('No direct script access allowed');

class IndexControl extends Control{
	function __auto(){
        C('debug',0);
		$template = __ROOT__ . '/template/' . C('tpl_style');
        $this->assign("template", $template);
	}
    // 显示首页
    function index(){
    	$sql='select value from ec_config where cid=5';
    	$num=M()->query($sql);
        //判断网站是否关闭(0为关闭1为开启)
    	if($num[0]['value']==0){
    		header("Content-type:text/html;charset=utf-8");
        	echo "<div style='font:36px/38px 微软雅黑;text-align:center;color:#333;border-bottom:solid 1px #eee;width:660px;height:30px;padding:20px 0px;'>:-) 系统正在维护,请稍候访问..........</div>";
			die;
    	}
        $this->display();
    }
    // 列表页
    function cat(){
        $cid = intval($_GET['cid']);
        if ($cid) {
            $db = M("category");
            $catData = $db->find($cid);
            //当前栏目信息分配到模板
            $this->assign("field",$catData);
            $this->display($catData['cattpl']);
        } else {
            go(__ROOT__);
        }
    }
    // 内容页
    function arc(){
        $aid = @intval($_GET['aid']);
        if ($aid) {
            $db = M("arc");
            $field = $db->find($aid);
            $cat = $db->table("category")->find($field['cid']);
            $this->assign("field",$field);
            $this->display($cat['arctpl']);
        }
    }
    //更多栏目内容
    function more(){
        $cid=intval($_GET['cid']);
        $db=M('arc');
        $page=new page($db->where("cid=$cid")->count(),6);
        $arcs=$db->where("cid=$cid")->limit($page->limit())->all();
        $this->assign('page',$page->show(2));
        $this->assign('arcs',$arcs);
        $this->display('news');    
    }

    // 检测用户名是否存在
    public function checkuser(){
    $db = M('user');
    $users = $db->all();
    foreach($users as $v){
        if($_POST['username']==$v['username']){
            echo json_encode(array('status' =>1));
            exit(); 
        }
    }       
    echo json_encode(array('status' => 0));
    exit();
    }



    //执行注册
public function adduser(){
    if(strtoupper($_POST['code']) !=strtoupper($_SESSION['code'])){
        echo json_encode(array('status' => 2));
        die();
        }
    $db = M('user');
    $_POST['password']=md5($_POST['password']);
    $_POST['time']=time();
    if($id=$db->add()){
        $_SESSION['username']=$_POST['username'];
        echo json_encode(array('status' => 1));
    }else{
        echo json_encode(array('status' => 0));
    }
}

//执行登录
    function login(){
        $db = M('user');
        $username = $this->_POST("username");
        if($username){
            if($user = $db->find("username='$username'")){
                if($user['password'] ==md5($_POST['password'])){
                    $_SESSION['userid']=$user['userid'];
                    $_SESSION['username']=$user['username'];
                    echo json_encode(array('status' => 1));//用户登录成功
                }else{
                    echo json_encode(array('status' => 2));//用户密码错误
                }
            }else{
                echo json_encode(array('status' => 0));//用户名不存在
            }
        }
    }
    //获取验证码
    public function code(){
        $code=new code();
        $code->show();
    }

    // 进入会员中心验证
    public function myinfo(){
        if(isset($_SESSION['userid'])){
            go('http://localhost/hb/easycms/member.php/');
        }else{
            go('index');
        }
    }





}
?>